﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Common;
using Garlic.CC.Utility;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using Garlic.DM.DataModel;

namespace Garlic.DAL
{
    /// <summary>
    /// DataAccess
    /// </summary>
    public partial class MenuDAL
    {
        public const string GETMENUS = "select DISTINCT " +
                                            "menu.menu_name," +
                                            "menu.parent_menu_id," +
                                            "menu.order_id," +
                                            "menu.image_url," +
                                            "menu.link_url," +
                                            "menu.comments," +
                                            "menu.create_user," +
                                            "menu.create_time," +
                                            "menu.modify_user," +
                                            "menu.modify_time," +
                                            "menu.menu_id" +
                                            "  from TM_BAS_Menu menu,TS_BAS_Auth auth,TS_BAS_Role role,TR_BAS_User_Role rUserRole,TS_BAS_User usr " +
            " where usr.user_id=:UserID and usr.user_id=rUserRole.user_id and role.role_id=rUserRole.role_id and role.role_id=auth.role_id and " +
            " auth.resource_id=menu.menu_id and auth.resource_type='Menu' and menu.parent_menu_id=:ParentMenuID order by menu.order_id";

        public const string GETMENUSBYROLEID = "select DISTINCT " +
                                            "menu.menu_name," +
                                            "menu.parent_menu_id," +
                                            "menu.order_id," +
                                            "menu.image_url," +
                                            "menu.link_url," +
                                            "menu.comments," +
                                            "menu.create_user," +
                                            "menu.create_time," +
                                            "menu.modify_user," +
                                            "menu.modify_time," +
                                            "menu.menu_id" +
                                            "  from TM_BAS_Menu menu,TS_BAS_Auth auth,TS_BAS_Role role " +
            " where role.role_id=auth.role_id and role.role_id=:RoleID and " +
            " auth.resource_id=menu.menu_id and auth.resource_type='Menu' order by menu.order_id";

        public const string GETMENUSBYACTIONID = "select DISTINCT " +
                                            "menu.menu_name," +
                                            "menu.parent_menu_id," +
                                            "menu.order_id," +
                                            "menu.image_url," +
                                            "menu.link_url," +
                                            "menu.comments," +
                                            "menu.create_user," +
                                            "menu.create_time," +
                                            "menu.modify_user," +
                                            "menu.modify_time," +
                                            "menu.menu_id" +
                                            "  FROM TS_BAS_Action,TR_BAS_Menu_Action,TM_BAS_Menu menu" +
            " where menu.menu_id=TR_BAS_Menu_Action.menu_id and " +
            " TR_BAS_Menu_Action.action_id=TS_BAS_Action.action_id and TS_BAS_Action.action_id=:ActionID order by menu.order_id";

        /// <summary>
        /// 获取顶级菜单
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static List<MenuInfo> GetTopMenus(int userID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(GETMENUS);
            db.AddInParameter(dbCommand, "UserID", DbType.Int32, userID);
            db.AddInParameter(dbCommand, "ParentMenuID", DbType.Int32, 0);

            return MenuDAL.GetList(dbCommand, db);
        }

        /// <summary>
        /// 获取指定的子菜单
        /// </summary>
        /// <param name="user"></param>
        /// <param name="parentMenuID"></param>
        /// <returns></returns>
        public static List<MenuInfo> GetSubMenus(int userID, int parentMenuID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(GETMENUS);
            db.AddInParameter(dbCommand, "UserID", DbType.Int32, userID);
            db.AddInParameter(dbCommand, "ParentMenuID", DbType.Int32, parentMenuID);

            return MenuDAL.GetList(dbCommand, db);
        }

        /// <summary>
        /// Get data list
        /// </summary>
        /// <param name="dbCommand"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static List<MenuInfo> GetList(DbCommand dbCommand,Database db)
        {
            List<MenuInfo> list = new List<MenuInfo>();
            using (IDataReader dr = db.ExecuteReader(dbCommand))
            {
                while (dr.Read())
                {
                    list.Add(CreateMenuInfo(dr));
                }
            }

            return list;
        }

        public static void DeleteActionAboutMenu(int menuid)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase();
                string msql = "delete TR_BAS_Menu_Action where menu_id = @menuids";
                DbCommand dbCommand = db.GetSqlStringCommand(msql);
                db.AddInParameter(dbCommand, "menuid", DbType.Int32, menuid);

                db.ExecuteNonQuery(dbCommand);
            }
            catch (System.Exception ex)
            {
                throw;
            }
        }

        /// <summary>
        /// 删除菜单，flag=true为级联删除
        /// </summary>
        /// <param name="menu_id"></param>
        /// <param name="flag"></param>
        public static void Delete(int menu_id, bool flag)
        {
            if (!flag)
            {
                MenuDAL.Delete(menu_id);
                AuthDAL.DeleteAuthAboutResource(menu_id, "Menu");
                MenuDAL.DeleteActionAboutMenu(menu_id);
            }
            else
            {
                List<MenuInfo> subMenus = GetList(" parent_menu_id=" + menu_id.ToString(), string.Empty);

                foreach (MenuInfo menu in subMenus)
                {
                    MenuDAL.Delete(menu.Menu_id);

                    AuthDAL.DeleteAuthAboutResource(menu.Menu_id, "Menu");
                    MenuDAL.DeleteActionAboutMenu(menu.Menu_id);
                }

                Delete(menu_id);
            }
        }

        

        public static void RemoveToRole(object menuID, int roleID)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public static List<MenuInfo> GetMenusByRoleID(int roleID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(GETMENUSBYROLEID);
            db.AddInParameter(dbCommand, "RoleID", DbType.Int32, roleID);

            return MenuDAL.GetList(dbCommand, db);
        }

        public static List<MenuInfo> GetMenusByActionID(int actionID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(GETMENUSBYACTIONID);
            db.AddInParameter(dbCommand, "ActionID", DbType.Int32, actionID);

            return MenuDAL.GetList(dbCommand, db);
        }
    }
}
